Jibé Barth

🇫🇷 Web developer

PHPInsights v2 released

Published Jun 21, 2021

In case you missed the following tweet, let me introduce the new v2.0 for PHPInsights.

People using PHP Insights: @jibbarth is making the final preparations for v2 — this release will feature:

✓ An "--fix" option that applies the fixes proposed by @phpinsights automatically. 🔥
✓ Blazing fast analysis by using cache and all your CPU cores. 🚀
✓ And more! pic.twitter.com/K4w5rNFpwr

— Nuno Maduro (@enunomaduro) April 24, 2021

This new version was mainly focused to improve performances, but it came also with new shiny features ✨

Let's discover them together.

Speed up the analysis process 🚀

Back in march 2020, I started to learn using Blackfire by following this course on SymfonyCast. Just after that, I decided to dig into PHPInsights to find ways to improve performances.

With this great tool, Oliver and I found some bottleneck, and we started to gain huge performances !

First run of performances gain

The next step was to add cached results.

When we run twice an analysis, all files are reanalyzed. What a boring process. As files doesn't change, the tool should keep the results.

Thanks to symfony/cache, we can now store all issues details in cache, and avoid recheck a file if it's content doesn't change between two analysis.

Cache usage

Finally, the most powerful tweak we do to improve performances is allow to use multiples CPU cores !

A new options is available in the phpinsights.php configuration file : threads. Specify here how many core you want to use. If you don't specify it, the tool is able to detect how many CPU core your computer has, and use them all.

See this example, with an htop profiling CPU core at the bottom :

PHPInsights Parallelization

Thanks to all theses improvements, we increase performance of 93% 🚀

Final blackfire comparison

Auto-fixer 🌟

As lot of Insights came from PHPCS or PHP-CS-Fixer, you can now fix them automatically. Just append the --fix option, and 🎉. After the analysis report, you'll get what has been fixed.

If you don't want to get the full report, just launch vendor/bin/phpinsights fix.

PHPInsights autofixer

Better feedbacks on your CI 🤖

@50bhan provide a way to specify multiple paths or files to analyze. This great feature allow you to use report only issues on changed files for a Pull Request with our GithubAction formatter :

  - name: PHPInsight on new/modified files
    if: github.event_name == 'pull_request'
    run: |
        URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${{ github.event.pull_request.number }}/files"
        # Retrieve list of add/modified files
        FILES=$(curl -s -X GET -G $URL | jq -r '.[] |  select( .status == "added" or .status == "modified") | select(.filename|endswith(".php")) | .filename')
        # Launch phpinsights against theses files
        php vendor/bin/phpinsights analyse $FILES --ansi -v -n --format=github-action

feedback on PRs

For peoples that don't use GithubAction but prefer Jenkins, @dsamburskyi improved the Checkstyle report, that allow the Warning NG plugin to correctly display it. Jenkins users, enjoy ✨

Warning NG on Jenkins

Finally, for Gitlab users, @guywarner add a new formatter to display results for codeclimate.

#.gitlab-ci.yml
insights:
  script:
    - vendor/bin/phpinsights -n --ansi --format=codeclimate > codeclimate-report.json
  artifacts:
    reports:
      codequality: codeclimate-report.json

Dependencies update 🔧

In this version, PHPInsights is fully compatible with PHP8 and Composer2. We also drop abandonned packages and upgrade to latest version of Slevomat/Coding-Standard and PHP-CS-Fixer. Thank you @50bhan for handling this 💪

Some Insights are already aware when you use PHP8 and are able to detect old fashion usage that could be refactored. For example, in PHP8, you can use ::class on every object instead calling the get_class() function.

php8 aware

Real time monitor 📈

A new --summary option is now available. By running following code, you can get an real time monitor of state of your code

watch -c -b php vendor/bin/phpinsights --summary --ansi

📈Do you want a Real-Time monitor of @phpinsights while you have a coding session ?

This PR might be helpful 😉 https://t.co/BT1md5quyj pic.twitter.com/2lrRNxmGWD

— JiBé Barth (@jibbarth) April 28, 2021

And lots of few bugs fixed, thanks of the awesome community 🙌:

I strongly encourage you to browse the full changelog.

We hope you'll enjoy this new release 😊

What's coming next ?

We've few ideas for the following releases.

We thought about integrate Rector, and @50bhan already start working on this.

I would also love to quickly import sets from PHP-CS-Fixer to automatically add batch of rules pre-configured.

Other idea ? Create an issue or send a Pull Request : https://github.com/nunomaduro/phpinsights